package com.psg.interceptor;

import com.psg.pojo.Look;
import com.psg.pojo.Produce;
import com.psg.pojo.User;
import com.psg.service.ProduceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class CustomerInterceptor implements HandlerInterceptor {
    @Autowired
    private ProduceService produceService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("开始--拦截器执行了！！！！");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("中间--执行！！！！");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        Produce produce = (Produce) request.getSession().getAttribute("produce");
        User user = (User) request.getSession().getAttribute("loginUser");
        Look look = new Look();
        System.out.println("PId是："+produce.getpId());
        look.setProductId(produce.getpId());
        //look.setLookId(user.getUId());
        System.out.println("Price是："+produce.getPrice());
        look.setLookPrice(produce.getPrice());
        look.setLookId(1);
        System.out.println("Weight是:"+produce.getWeight());
        look.setLookWeight(produce.getWeight());
        System.out.println(look);
        produceService.recordProduce(look);
        System.out.println("视图加载完毕返回！！！");
    }
}
